/**
 * 最长奇偶子数组
 *
 * @USER: Administrator
 * @DATE: 2024/9/28 上午9:57
 * @VERSION: 1.0.0
 */
public class LC2760 {
    public static void main(String[] args) {
        LC2760 lc2760 = new LC2760();
        System.out.println(lc2760.longestAlternatingSubarray(new int[]{2,2},10));
    }

    public int longestAlternatingSubarray(int[] nums, int threshold) {
        int longest = 0;
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] %2 == 0 && nums[i] <= threshold) {
                count++;
                if (count > longest) {
                    longest = count;
                }
                int j = i + 1;
                for (; j < nums.length; j++) {
                    if (nums[j] %2 != nums[j - 1] % 2 && nums[j] <= threshold) {
                        count++;
                        if (count > longest) {
                            longest = count;
                        }
                    }else {
                        count = 0;
                        break;
                    }
                }
                i = j-1;
            }
        }
        return longest;
    }

}
